package com.zygh.lz.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zygh.lz.entity.VehicleLocation;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.security.core.parameters.P;

import java.util.List;
import java.util.Map;

@Mapper
public interface VehicleLocationMapper extends BaseMapper<VehicleLocation> {

    @Insert("REPLACE INTO `lz`.`vehicle_location` (`gpsid`, `longitude`, `latitude`, `upload_time`) " +
            "VALUES (#{gpsid}, #{longitude}, #{latitude}, now())")
    int addVehicleLocation(VehicleLocation vehicleLocation);

    @Select("<script>" +
            "SELECT vl.*,ve.picture_type as pictureType,ve.car_no as carNo,ve.org_name as orgName " +
            "FROM vehicle_location vl LEFT JOIN vehicle_equipment ve ON vl.gpsid = ve.gpsid " +
            " <where> " +
            "   <if test = 'type != null'> " +
            "       picture_type = #{type} " +
            "   </if> " +
            "   <if test = 'sectionName != null'> " +
            "      AND org_name LIKE CONCAT('%',#{sectionName},'%') " +
            "   </if>" +
            "      AND TIMESTAMPDIFF(MINUTE,upload_time,NOW()) &lt;= 60 " +
            "</where>" +
            "</script>")
    List<Map<String, Object>> findVehicleLocation(@Param("sectionName") String sectionName,
                                                  @Param("type") String type);
}
